/*
 * @Author: dfh
 * @Date: 2024-10-15 10:13:14
 * @LastEditors: dfh
 * @Modified By: dfh
 * @describe: 
 */
import 'package:flutter_pc_manager/app/modules/statistics_manager/models/turnover_model.dart';
import 'package:flutter_pc_manager/app/modules/statistics_manager/models/user_model.dart';
import 'package:flutter_pc_manager/app/modules/statistics_manager/provider/statistics_manger_provider.dart';
import 'package:get/get.dart';

class StatisticsManagerController extends GetxController {
  final StatisticsManagerProvider _provider = StatisticsManagerProvider();
  final navIndex = 0.obs;
  final turnoverData = TurnoverVo(dateList: '', turnoverList: '').obs;
  final turnoverX = Rx<List<String>>([]);
  final turnoverY = Rx<List<String>>([]);

  // final userData = UserVo(dateList: '', totalUserList: '', newUserList: '');
  final userX = Rx<List<String>>([]);
  final totalUserY = Rx<List<String>>([]);
  final newUserY = Rx<List<String>>([]);


  void fetchTurnoverData(String begin, String end) async {
    try {
      dynamic result = await _provider.fetchTurnoverList(begin, end);
      int code = result['code'];
      if (code == 1) {
        TurnoverVo data = TurnoverVo.fromJson(result['data']);
        turnoverData.value = data;
        turnoverX.value = data.dateList.split(',');
        turnoverY.value = data.turnoverList.split(',');
      } else {
        String msg = result['msg'];
        Get.snackbar('错误', msg);
      }
    } catch (e) {
      dynamic error = e;
      Get.snackbar('错误', error.message ?? '网络异常');
    }
  }

  void fetchUserData(String begin, String end) async {
    try {
      dynamic result = await _provider.fetchUserList(begin, end);

      int code = result['code'];
      if (code == 1) {
        UserVo data = UserVo.fromJson(result['data']);
        print("用户统计数据：${data.toJson()}");
        userX.value = data.dateList.split(',');
        totalUserY.value = data.totalUserList.split(',');
        newUserY.value = data.newUserList.split(',');
      } else {
        String msg = result['msg'];
        Get.snackbar('错误', msg);
      }
    } catch (e) {
      dynamic error = e;
      Get.snackbar('错误', error.message ?? '网络异常');
    }
  }
}
